﻿<UserControl x:Class="SaryaAccounting.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <SolidColorBrush x:Key="LightColor" Color="#FF8ACA06" />
        <SolidColorBrush x:Key="BgLightColor" Color="#5D8ACA06" />
        <SolidColorBrush x:Key="DisabledBackgroundColor" Color="#FFEEEEEE" />
        <SolidColorBrush x:Key="GlyphColor" Color="#FF444444" />
        <SolidColorBrush x:Key="DisabledForegroundColor" Color="#FF888888" />

        <MenuScrollingVisibilityConverter x:Key="MenuScrollingVisibilityConverter" />

        <Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="{x:Type Separator}">
            <Setter Property="Height" Value="1" />
            <Setter Property="Margin" Value="0,4,0,4" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Separator}">
                        <Border BorderThickness="1" BorderBrush="{StaticResource LightColor}" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <ControlTemplate x:Key="{x:Static MenuItem.TopLevelHeaderTemplateKey}" TargetType="{x:Type MenuItem}">
            <Border Name="border">
                <Grid>
                    <ContentPresenter Margin="6,3,6,3" ContentSource="Header" RecognizesAccessKey="True" />
                    <Popup x:Name="popup" Placement="Bottom" IsOpen="{TemplateBinding IsSubmenuOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Fade">
                        <Border x:Name="submenuBorder" SnapsToDevicePixels="True" BorderThickness="1" Background="White" BorderBrush="{StaticResource LightColor}">
                            <ScrollViewer CanContentScroll="True" Style="{StaticResource MenuScrollViewer}">
                                <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" />
                            </ScrollViewer>
                        </Border>
                    </Popup>
                </Grid>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsSuspendingPopupAnimation" Value="True">
                    <Setter TargetName="popup" Property="PopupAnimation" Value="None" />
                </Trigger>
                <Trigger Property="IsHighlighted" Value="True">
                    <Setter TargetName="border" Property="BorderBrush" Value="Transparent" />
                    <Setter TargetName="border" Property="Background" Value="{StaticResource BgLightColor}" />
                </Trigger>
                <Trigger SourceName="popup" Property="AllowsTransparency" Value="True">
                    <Setter TargetName="submenuBorder" Property="CornerRadius" Value="0,0,4,4" />
                    <Setter TargetName="submenuBorder" Property="Padding" Value="0,0,0,3" />
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Foreground" Value="{StaticResource DisabledBackgroundColor}" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

        <ControlTemplate x:Key="{x:Static MenuItem.TopLevelItemTemplateKey}" TargetType="{x:Type MenuItem}">
            <Border x:Name="border">
                <Grid>
                    <ContentPresenter Margin="6,3,6,3" ContentSource="Header" RecognizesAccessKey="True" />
                </Grid>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsHighlighted" Value="True">
                    <Setter TargetName="border" Property="Background" Value="{StaticResource BgLightColor}" />
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Foreground" Value="{StaticResource DisabledBackgroundColor}" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

        <ControlTemplate x:Key="{x:Static MenuItem.SubmenuItemTemplateKey}" TargetType="{x:Type MenuItem}">
            <Border x:Name="border" BorderThickness="1">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" SharedSizeGroup="Icon" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" SharedSizeGroup="Shortcut" />
                        <ColumnDefinition Width="13" />
                    </Grid.ColumnDefinitions>
                    <ContentPresenter x:Name="Icon" Margin="6,0,6,0" VerticalAlignment="Center" ContentSource="Icon" />
                    <Border x:Name="Check" Width="13" Height="13" Visibility="Collapsed" Margin="6,0,6,0" BorderThickness="1" BorderBrush="{StaticResource LightColor}">
                        <Path x:Name="CheckMark" Width="7" Height="7" Visibility="Hidden" SnapsToDevicePixels="False" StrokeThickness="2" Data="M 0 0 L 7 7 M 0 7 L 7 0" Stroke="{StaticResource GlyphColor}" />
                    </Border>
                    <ContentPresenter x:Name="HeaderHost" Grid.Column="1" ContentSource="Header" RecognizesAccessKey="True" />
                    <TextBlock x:Name="InputGestureText" Grid.Column="2" Text="{TemplateBinding InputGestureText}" Margin="5,2,0,2" DockPanel.Dock="Right" />
                </Grid>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="Icon" Value="{x:Null}">
                    <Setter TargetName="Icon" Property="Visibility" Value="Hidden" />
                </Trigger>
                <Trigger Property="IsChecked" Value="true">
                    <Setter TargetName="CheckMark" Property="Visibility" Value="Visible" />
                </Trigger>
                <Trigger Property="IsCheckable" Value="true">
                    <Setter TargetName="Check" Property="Visibility" Value="Visible" />
                    <Setter TargetName="Icon" Property="Visibility" Value="Hidden" />
                </Trigger>
                <Trigger Property="IsHighlighted" Value="True">
                    <Setter TargetName="border" Property="Background" Value="{StaticResource BgLightColor}" />
                    <Setter TargetName="border" Property="BorderBrush" Value="{StaticResource LightColor}" />
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Foreground" Value="{StaticResource DisabledBackgroundColor}" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

        <ControlTemplate x:Key="{x:Static MenuItem.SubmenuHeaderTemplateKey}" TargetType="{x:Type MenuItem}">
            <Border x:Name="Border" BorderThickness="1">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" SharedSizeGroup="Icon" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" SharedSizeGroup="Shortcut" />
                        <ColumnDefinition Width="13" />
                    </Grid.ColumnDefinitions>
                    <ContentPresenter x:Name="Icon" Margin="6,0,6,0" VerticalAlignment="Center" ContentSource="Icon" />
                    <ContentPresenter x:Name="HeaderHost" Grid.Column="1" ContentSource="Header" RecognizesAccessKey="True" />
                    <TextBlock x:Name="InputGestureText" Grid.Column="2" Text="{TemplateBinding InputGestureText}" Margin="5,2,2,2" DockPanel.Dock="Right" />
                    <Path Grid.Column="3" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 0 7 L 4 3.5 Z" Fill="{StaticResource GlyphColor}" />
                    <Popup x:Name="Popup" Placement="Right" HorizontalOffset="-4" IsOpen="{TemplateBinding IsSubmenuOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Fade">
                        <Border x:Name="SubmenuBorder" SnapsToDevicePixels="True" Background="{DynamicResource MenuPopupBrush}" BorderThickness="1" BorderBrush="{StaticResource LightColor}">
                            <ScrollViewer CanContentScroll="True" Style="{StaticResource MenuScrollViewer}">
                                <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" />
                            </ScrollViewer>
                        </Border>
                    </Popup>
                </Grid>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="Icon" Value="{x:Null}">
                    <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
                </Trigger>
                <Trigger Property="IsHighlighted" Value="true">
                    <Setter Property="Background" TargetName="Border" Value="{StaticResource BgLightColor}" />
                    <Setter Property="BorderBrush" TargetName="Border" Value="{StaticResource LightColor}" />
                </Trigger>
                <Trigger SourceName="Popup" Property="AllowsTransparency" Value="True">
                    <Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="4" />
                    <Setter TargetName="SubmenuBorder" Property="Padding" Value="0,3,0,3" />
                </Trigger>
                <Trigger Property="IsEnabled" Value="false">
                    <Setter Property="Foreground" Value="{StaticResource DisabledForegroundColor}" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

        <Style x:Key="MenuScrollViewer" TargetType="{x:Type ScrollViewer}" BasedOn="{x:Null}">
            <Setter Property="HorizontalScrollBarVisibility" Value="Hidden" />
            <Setter Property="VerticalScrollBarVisibility" Value="Auto" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ScrollViewer}">
                        <Grid SnapsToDevicePixels="True">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <Border Grid.Row="1" Grid.Column="0">
                                <ScrollContentPresenter Margin="{TemplateBinding Padding}" />
                            </Border>
                            <RepeatButton Style="{StaticResource MenuScrollButton}" Grid.Row="0" Grid.Column="0" Command="{x:Static ScrollBar.LineUpCommand}" CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Focusable="False">
                                <RepeatButton.Visibility>
                                    <MultiBinding FallbackValue="Visibility.Collapsed" Converter="{StaticResource MenuScrollingVisibilityConverter}" ConverterParameter="0">
                                        <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="ComputedVerticalScrollBarVisibility" />
                                        <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="VerticalOffset" />
                                        <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="ExtentHeight" />
                                        <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="ViewportHeight" />
                                    </MultiBinding>
                                </RepeatButton.Visibility>
                                <Path Fill="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}" Data="{StaticResource UpArrow}" />
                            </RepeatButton>
                            <RepeatButton Style="{StaticResource MenuScrollButton}" Grid.Row="2" Grid.Column="0" Command="{x:Static ScrollBar.LineDownCommand}" CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}" Focusable="False">
                                <RepeatButton.Visibility>
                                    <MultiBinding FallbackValue="Visibility.Collapsed" Converter="{StaticResource MenuScrollingVisibilityConverter}" ConverterParameter="100">
                                        <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="ComputedVerticalScrollBarVisibility" />
                                        <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="VerticalOffset" />
                                        <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="ExtentHeight" />
                                        <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="ViewportHeight" />
                                    </MultiBinding>
                                </RepeatButton.Visibility>
                                <Path Fill="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}" Data="{StaticResource DownArrow}" />
                            </RepeatButton>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </UserControl.Resources>
    <Grid>
        <Rectangle Stroke="Green" Fill="Beige" Width="20" Height="20">
            <Rectangle.ContextMenu>
                <ContextMenu>
                    <ContextMenu.Style>
                        <Style TargetType="{x:Type ContextMenu}">
                            <Setter Property="OverridesDefaultStyle" Value="True" />
                            <Setter Property="SnapsToDevicePixels" Value="True" />
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type ContextMenu}">
                                        <Border BorderThickness="1">
                                            <StackPanel ClipToBounds="True" Orientation="Horizontal" IsItemsHost="True" />
                                        </Border>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ContextMenu.Style>
                    <MenuItem Header="Save" />
                    <MenuItem Header="Save And Close" />
                    <MenuItem Header="Save And New" />
                </ContextMenu>
            </Rectangle.ContextMenu>
        </Rectangle>
    </Grid>
</UserControl>
